//
//  ExplicitViewController.swift
//  Animator
//
//  Created by liu guoqiang on 16/9/10.
//  Copyright © 2016年 liu guoqiang. All rights reserved.
//

import UIKit

class ExplicitViewController: UIViewController {
    @IBOutlet weak var plane: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.plane.layer.opacity = 0.25
    }
    @IBAction func implicitClick(sender: AnyObject) {
        let moveTransform = CGAffineTransformMakeTranslation(180, 200)
        self.plane.layer.setAffineTransform(moveTransform)
        self.plane.layer.opacity = 1;
    }

    @IBAction func explicitClick(sender: AnyObject) {
        let opAnim = CABasicAnimation(keyPath: "opacity")
        opAnim.duration = 3.0
        opAnim.fromValue = 0.25
        opAnim.toValue = 1.0
        opAnim.cumulative = true
        opAnim.repeatCount = 2
        opAnim.fillMode = kCAFillModeForwards
        opAnim.removedOnCompletion = false
        self.plane.layer.addAnimation(opAnim, forKey: "animateOpacity")
        
        let moveTransform = CGAffineTransformMakeTranslation(180, 200)
        let moveAnim = CABasicAnimation(keyPath: "transform")
        moveAnim.duration = 6.0
        moveAnim.toValue = NSValue(CATransform3D: CATransform3DMakeAffineTransform(moveTransform))
        moveAnim.fillMode = kCAFillModeForwards
        moveAnim.removedOnCompletion = false
        self.plane.layer.addAnimation(moveAnim, forKey: "animateTransform")
        opAnim.fillMode = kCAFillModeForwards
        opAnim.removedOnCompletion = false
        
    }
    
    @IBAction func keyFrameClick(sender: AnyObject) {
        let opAnim = CAKeyframeAnimation(keyPath: "opacity")
        opAnim.duration = 6.0
        opAnim.values = [0.25,0.75,1.0]
        opAnim.keyTimes = [0.0,0.5,1.0]
        opAnim.fillMode = kCAFillModeForwards
        opAnim.removedOnCompletion = false
        self.plane.layer.addAnimation(opAnim, forKey: "animateOpacity")
        
        let moveTransform = CGAffineTransformMakeTranslation(180, 200)
        let moveAnim = CABasicAnimation(keyPath: "transform")
        moveAnim.duration = 6.0
        moveAnim.toValue = NSValue(CATransform3D: CATransform3DMakeAffineTransform(moveTransform))
        moveAnim.fillMode = kCAFillModeForwards
        moveAnim.removedOnCompletion = false
        self.plane.layer.addAnimation(moveAnim, forKey: "animateTransform")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}
